import org.bytedeco.javacpp.*;
import org.bytedeco.opencv.global.opencv_imgcodecs;
import org.bytedeco.opencv.opencv_core.*;

import static org.bytedeco.opencv.global.opencv_imgproc.*;

import org.bytedeco.tesseract.TessBaseAPI;

public class OCRService {


    public String doOCR(String imagePath) {

        TessBaseAPI tessBaseAPI = new TessBaseAPI();

        // 初始化Tesseract
        if (tessBaseAPI.Init(".", "ENG") != 0) {
            System.err.println("Could not initialize Tesseract.");
            return null;
        }

        // 读取图像文件
        Mat image = opencv_imgcodecs.imread(imagePath);
        // 图像预处理
        // 如调整大小、转为灰度图等
        cvtColor(image, image, COLOR_BGR2GRAY);

        // 设置图像
//        tessBaseAPI.SetImage(image.data(), image.cols(), image.rows(), 1, image.step());
        tessBaseAPI.SetImage(image.data(), image.cols(), image.rows(), 1, (int) image.step());

        // 识别文字
        BytePointer outText = tessBaseAPI.GetUTF8Text();
        String result = outText.getString();

        System.out.println(result);

        outText.deallocate();

        // 释放资源
        tessBaseAPI.End();

        return result;
    }


    public static void main(String[] args) {


//        OCRService ocrService = new OCRService();
//        ocrService.doOCR("E://zmj1.jpg");

        String dd = "qq";

        for (int i = 0; i < 20; i++) {
            System.out.println(Integer.valueOf(i).byteValue());
        }

        byte[] bytes = dd.getBytes();
        System.out.println(bytes.length);

    }

}
